import { createSSRApp } from "vue";
import App from "./App.vue";
import WZ from './commpent/wz.vue'
import Uploader from './commpent/uploader.vue'

import { createStore } from 'vuex'
import EventBus from './utils/event.js'
import format from './utils/format.js'
const $bus = new EventBus()
const store = createStore({ // https://mp.weixin.qq.com/s?src=11&timestamp=1650074859&ver=3741&signature=B4veyKkH-7hzE8cb3oUHzNXl0aaBHscNDTxiVtw5Jx6AWU443SZ4TNG0joch4z2Qkf59nM3auQhjko0FD1dtDScMdifZvIT4ZanTJEl42h-Pucnst4JH1iit33Vd9k2y&new=1
	state() {
		return {
			count: 0,
			wxUserInfo: {},
			apiUserInfo: {
				name: "wzskyline"
			},
			mapInfo: {},
			publishInfo: {},
			ztLocation: {},
			ztSpace: {},
			ztPrice: {},
			tmp: {},
		}
	},
	modules: {},
	mutations: {
		addCount(state, data) { state.count += data },
		asyncwxUserInfo(state, data) { state.wxUserInfo = Object.assign(state.wxUserInfo, data) },
		asyncApiUserInfo(state, data) { state.apiUserInfo = Object.assign(state.apiUserInfo, data) },
		asyncPublishInfo(state, data) { state.publishInfo = Object.assign(state.publishInfo, data) },
		asyncMapInfo(state, data) { state.mapInfo = Object.assign(state.mapInfo, data) },
		asyncTmp(state, data) { state.tmp = Object.assign(state.tmp, data) },
		asyncztLocation(state, data) { state.ztLocation = Object.assign(state.ztLocation, data) },
		asyncztSpace(state, data) { state.ztSpace = Object.assign(state.ztSpace, data) },
		asyncztPrice(state, data) { state.ztPrice = Object.assign(state.ztPrice, data) },

	},
	actions: {
		addAction({ commit }, data) { commit('addCount', data) },
		asyncwxUserInfo({ commit }, data) { commit('asyncwxUserInfo', data) },
		asyncApiUserInfo({ commit }, data) { commit('asyncApiUserInfo', data) },
		asyncPublishInfo({ commit }, data) { commit('asyncPublishInfo', data) },
		asyncMapInfo({ commit }, data) { commit('asyncMapInfo', data) },
		asyncTmp({ commit }, data) { commit('asyncTmp', data) },
		ztLocation({ commit }, data) { commit('asyncztLocation', data) },
		ztSpace({ commit }, data) { commit('asyncztSpace', data) },
		ztPrice({ commit }, data) { commit('asyncztPrice', data) },

	},

	getters: {
		getCount(state) {
			return `我是前端仔，在测试getters方法,获取count的值是：${state.count}`
		},
		getApiUserInfo(state) {
			return state.apiUserInfo
		},
		getMapInfo(state) {
			return state.mapInfo
		},
	}
})

export function createApp() {
	const app = createSSRApp(App);
	app.config.globalProperties.toPage = (url) => { wx.navigateTo({ url }) };
	app.config.globalProperties.$bus = $bus
	app.config.globalProperties.format = format 
	app.config.globalProperties.token = 'Bearer ' + "";
	app.config.globalProperties.ajax = {
		api: {
			wxLogin: '/user/wxLogin',
			queryBuildingsByPropertyAddress: `/location/queryBuildingsByPropertyAddress`,
			querySpaceByBuildinglocat: 'ion/querySpaceByBuilding',
			fileUpload: 'https://112.74.74.239/prod-api/wx/oss/upload',

			property: '/property',
			propertys: '/property/list',
			listByAuth: '/property/listByAuth',
			propertyDel: '/property/id',
			propertyDatil: '/property/id',
			addTemp: '/property/addTemp',
			loadTemp: '/property/loadTemp',
			getUserInfo: '/user/getInfo',
			genCode: '/invitationCode/genCode',
			updateUser: '/user/updateUser',
			submitInvitationCode: '/user/submitInvitationCode',
		},
		baseOptions(params, method = 'GET') {
			console.log('params', params) 
			return new Promise((resolve, reject) =>{   
				return wx.request({
					isShowLoading: false,
					loadingText: '正在加载',
					url: "https://112.74.74.239/prod-api/wx" + params.url,
					data: params.data,
					method: method,
					header: { 'content-type': params.contentType || 'application/json', 'Authorization': app.config.globalProperties.token },
					success(res) {
						if (params.url.includes("wxLogin")) {
							console.log("登录后的响应", res);
							console.log("登录后的响应", res.data.data);
							//var token = 'Bearer ' + res.data.data.token;
							//console.log("登录后的响应 TOKEN= ", token);
							//wx.setStorageSync('token', token)
							//app.config.globalProperties.token = token
	
						}
						resolve(res.data) 
					},
					error(e) {
						console.log('api', '请求接口出现问题', e)
						reject(e)
					}
				}) 
			 })    
		},
		get(url, data = '') {
			return this.baseOptions({ url, data })
		},
		post: function (url, data) {
			return this.baseOptions({ url, data }, 'POST')
		},
		upload: function (url, data) {
			return this.baseOptions({ url, data, contentType: 'multipart/form-data' }, 'POST')
		}
	}
	app.use(store)
	return {
		app,
	};
}
